﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class ConfigrationCameraService : IService<ConfigrationCamera>
    {
        private readonly IEntityRepository<ConfigrationCamera> _configrationCameraRepository;
        public ConfigrationCameraService(IEntityRepository<ConfigrationCamera> configrationCameraRepository)
        {
            _configrationCameraRepository = configrationCameraRepository;
        }

        public ConfigrationCamera Create(ConfigrationCamera t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _configrationCameraRepository.InsertOnCommit(t);
                _configrationCameraRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public ConfigrationCamera Update(ConfigrationCamera t)
        {
            ConfigrationCamera item;
            try
            {
                item = _configrationCameraRepository.Get(t.Id);
                item.ComputerName = t.ComputerName;
                item.Server = t.Server;
                item.UserID = t.UserID;
                item.Password = t.Password;
                item.Cam1 = t.Cam1;
                item.Cam2 = t.Cam2;
                item.CardReader = t.CardReader;
                _configrationCameraRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public ConfigrationCamera Detail(Guid id)
        {
            var item = _configrationCameraRepository.Get(id);
            return item;
        }

        public ConfigrationCamera Detail(int id)
        {
            var item = _configrationCameraRepository.Get(id);
            return item;
        }

        public ConfigrationCamera Detail(string id)
        {
            var item = _configrationCameraRepository.Get(id);
            return item;
        }

        public List<ConfigrationCamera> ToList()
        {
            var item = _configrationCameraRepository.GetAll().ToList();
            return item;
        }

        public void Delete(ConfigrationCamera t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _configrationCameraRepository.DeleteOnCommit(t);
                _configrationCameraRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
